package com.teemor.im.remoting.netty;

import com.alibaba.fastjson.JSON;
import com.teemor.im.common.command.RemoteCommand;
import com.teemor.im.common.util.JsonUtil;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToMessageCodec;
import lombok.extern.slf4j.Slf4j;

import java.util.List;

/**
 * @ename zhoulk
 * @cname 周李科
 * @date 2020/6/16 15:07
 */
@Slf4j
public class RmtMessageDecoder extends MessageToMessageCodec<String, RemoteCommand> {


    @Override
    protected void encode(ChannelHandlerContext ctx, RemoteCommand msg, List<Object> out) {
        out.add(JSON.toJSONString(msg) + System.getProperty("line.separator"));
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, String msg, List<Object> out) {
        out.add(JsonUtil.jsonToPojo(msg, RemoteCommand.class));
    }
}
